package com.swq.java.code.chapter19.code12;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FrameCounter extends JFrame implements ActionListener {
    private JButton button = new JButton("1");

    public FrameCounter(String title) {
        super(title);
        //把FrameCounter本身的实例注册为JButton的监听器
        button.addActionListener(this);
        add(button);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(100, 100);
        setVisible(true);
    }

    /**
     * 实现ActionListener的actionPerformed()方法
     */
    public void actionPerformed(ActionEvent evt) {
        int count = Integer.parseInt(button.getText());
        button.setText(new Integer(++count).toString());  //把JButton上的标号加1
    }

    public static void main(String args[]) {
        new FrameCounter("Hello");
    }
}
